<template>
  <div class="zero-gas-wrap">
    <Dropdown trigger="click" placement="bottom-start" @on-click="handleChangeFunc">
      <Button type="primary" size="small" :disabled="!showBtn">
        零用气
        <Icon type="ios-arrow-down"></Icon>
      </Button>
      <DropdownMenu slot="list">
        <template v-for="item in showList">
          <Dropdown placement="right-start" :name="item.value" :key="item.value" v-if="item.children">
            <DropdownItem>
              {{ item.label }}
              <Icon type="ios-arrow-forward"></Icon>
            </DropdownItem>
            <DropdownMenu slot="list">
              <DropdownItem v-for="sub of item.children" :name="sub.value" :key="sub.value">{{
                sub.label
              }}</DropdownItem>
            </DropdownMenu>
          </Dropdown>
          <DropdownItem :name="item.value" :key="item.value" v-else>
            {{ item.label }}
          </DropdownItem>
        </template>
      </DropdownMenu>
    </Dropdown>

    <!-- 连续零用气量 -->
    <Modal class="modal-p10" v-model="lxlyqVisible" title="连续用气量" width="360" @on-cancel="handleLxlyqModalCanel">
      <Form ref="lxlyqForm" :model="lxlyqForm" label-position="top" @submit.native.prevent="handleLxlyqQuery">
        <FormItem label="天数" prop="zeroDay">
          <InputNumber v-model="lxlyqForm.zeroDay" placeholder="请输入天数" style="width:100%" />
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="primary" @click="handleLxlyqQuery">确认</Button>
      </div>
    </Modal>

    <!-- 零用气量统计 -->
    <Modal
      v-model="lyqtjVisible"
      title="零用气量统计"
      width="95%"
      :styles="{ top: '30px', height: '85%' }"
      @on-cancel="handleLyqtjModalCanel"
    >
      <div :is="lyqtjTpl" :chartData="lyqtjData" :defaultOptions="lyqfbtjOptions"></div>
      <div slot="footer">
        <Button type="primary" @click="handleLyqtjModalCanel">确认</Button>
      </div>
    </Modal>

    <!-- C端入口 -->
    <Modal
      class="cdrk-modal"
      v-model="cdrkModalVisible"
      title="C端入口数据"
      width="95%"
      :styles="{ top: '30px', height: '85%' }"
      @on-cancel="handleCdrkModalCanel"
    >
      <div class="chart-box">
        <div :is="cdrkTpl" :defaultOptions="cdrkOptions" :chartData="cdrkChartData" />
      </div>
      <div slot="footer">
        <Button type="primary" @click="handleCdrkModalCanel">确认</Button>
      </div>
    </Modal>
  </div>
</template>
<script>
import { createNamespacedHelpers } from 'vuex'
import { each, merge } from 'lodash'
import { mainMixins } from '../mixins'
import { lyqfbtjOptions, cdrkOptions } from './chartOptions'

const { mapState, mapActions } = createNamespacedHelpers('historyVerify')

export default {
  name: 'history-verify-zero-gas',
  mixins: [mainMixins],
  components: {
    HBar3d: () => import('@/commons/charts/HBar3d')
  },
  data() {
    return {
      funcToolList: {
        lxlyq: this.handleLxlyqlClick,
        hylxlyq: this.handleLxlyqlCancel,
        lyqfbtj: this.handleLyqfbtjClick,
        cdrksj: this.handleCdrksjClick
      },
      lxlyqVisible: false,
      lxlyqForm: {
        zeroDay: 0
      },
      lyqtjVisible: false,
      lyqtjTpl: null,
      lyqltjList: [],
      cdrkModalVisible: false,
      cdrkTpl: null
    }
  },
  watch: {
    showCdrkModal() {
      this.cdrkModalVisible = this.showCdrkModal
      if (this.showCdrkModal) {
        this.cdrkTpl = 'HBar3d'
      } else {
        this.cdrkTpl = null
      }
    }
  },
  computed: {
    ...mapState(['lxlyqlState', 'cdrkList', 'showCdrkModal']),
    toolList() {
      return [
        {
          label: '连续零用气',
          value: 'lxlyq',
          active: !this.lxlyqlState
        },
        {
          label: '还原连续零用气',
          value: 'hylxlyq',
          active: this.lxlyqlState
        },
        {
          label: '零用气分布统计',
          value: 'lyqfbtj',
          active: true
        },
        {
          label: '外部数据引入',
          value: 'wbsjyr',
          active: true,
          children: [
            {
              label: 'C端入口数据',
              value: 'cdrksj',
              active: true
            },
            {
              label: '企业经营数据',
              value: 'qyjysj',
              active: true
            }
          ]
        }
      ]
    },
    showHPie() {
      return this.cdrkList.length > 0 && this.showCdrkModal
    },
    cdrkChartData() {
      const _serList = []
      const _cateList = []
      each(this.cdrkList, item => {
        const { name, value } = item
        _serList.push({
          name: name,
          y: value
        })
        _cateList.push(name)
      })
      return merge({}, cdrkOptions, {
        xAxis: {
          categories: _cateList
        },
        series: [
          {
            data: _serList
          }
        ]
      })
    },
    lyqfbtjOptions() {
      return lyqfbtjOptions
    },
    cdrkOptions() {
      return cdrkOptions
    },
    lyqtjData() {
      const _serList = []
      const _cateList = []
      each(this.lyqltjList, item => {
        const { name, value } = item
        _serList.push({
          name,
          y: value
        })
        _cateList.push(name)
      })
      return merge({}, lyqfbtjOptions, {
        xAxis: {
          categories: _cateList
        },
        series: [
          {
            data: _serList
          }
        ]
      })
    }
  },
  methods: {
    ...mapActions([
      'changeLxlyqlDay',
      'changeLxlyqlState',
      'changeWbsjSelectState',
      'changeCdrkModal',
      'loadLyqltjData'
    ]),
    handleLxlyqlClick() {
      this.lxlyqVisible = true
    },
    handleLxlyqModalCanel() {
      this.lxlyqVisible = false
      this.$refs['lxlyqForm'].resetFields()
    },
    handleLxlyqQuery() {
      const { zeroDay } = this.lxlyqForm
      if (zeroDay >= 2) {
        this.changeLxlyqlDay(zeroDay)
        this.handleLxlyqModalCanel()
      } else {
        this.$Notice.warning({
          title: '操作提示',
          desc: '连续天数必须大于1天'
        })
      }
    },
    handleLxlyqlCancel() {
      this.changeLxlyqlDay(0)
      this.changeLxlyqlState(false)
    },
    async handleLyqfbtjClick() {
      const { code, date } = await this.loadLyqltjData()
      if (code === 200) {
        this.lyqltjList = date.list
        this.lyqtjVisible = true
        this.lyqtjTpl = 'HBar3d'
      }
    },
    handleLyqtjModalCanel() {
      this.lyqtjVisible = false
      this.lyqtjTpl = null
      this.lyqltjList = []
    },
    handleCdrksjClick() {
      this.changeWbsjSelectState(true)
    },
    handleCdrkModalCanel() {
      this.changeCdrkModal(false)
    }
  }
}
</script>
<style lang="less" scoped>
.cdrk-modal {
  .chart-box {
    width: 100%;
    height: 100%;
    overflow: hidden;
  }
}
</style>
